package mall.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import mall.pojo.Cart;
import mall.pojo.Category;
import mall.pojo.Item;
import mall.pojo.Product;
import mall.service.CategoryService;
import mall.service.ProductService;
import mall.util.UtilityClass;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Scope("request")
public class CartController {

	@Autowired
	private ProductService productService;

	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private Cart cart;

	/*
	 * @RequestMapping(value = "/addcart", method = RequestMethod.GET) public
	 * 
	 * @ResponseBody Product addCart() { int productId = 19; Product product =
	 * productService.getProduct(productId); product.setName("abccs"); return
	 * new Product(); }
	 */

	@RequestMapping(value = "/addcart", method = RequestMethod.GET)
	public @ResponseBody
	Cart addCart(@RequestParam String productId) {
		Product product = productService
				.getProduct(Integer.parseInt(productId));
		Item item = new Item(product.getId(), product.getName(),
				product.getPrice(), 1, product.getPicture(), product.getPrice());
		List<Item> listItem = cart.getItems();

		// check if item contains in cart
		if (listItem.contains(item)) {// item contains in cart , just update
										// quantity
			Item existItem = listItem.get(listItem.indexOf(item));
			existItem.setQuantity(existItem.getQuantity() + 1);
			existItem.setTotal(existItem.getPrice() * existItem.getQuantity());
		} else {// add new item into cart
			listItem.add(item);
		}
		return cart;
	}

	@RequestMapping(value = "/addcartquantity", method = RequestMethod.GET)
	public ModelAndView addNumberCart(@ModelAttribute("id") int productId,
			@ModelAttribute("quantity") int quantity,HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		Product product = productService
				.getProduct(productId);
		Item item = new Item(product.getId(), product.getName(),
				product.getPrice(), 1, product.getPicture(), product.getPrice());
		List<Item> listItem = cart.getItems();

		// check if item contains in cart
		if (listItem.contains(item)) {// item contains in cart , just update
										// quantity
			Item existItem = listItem.get(listItem.indexOf(item));
			existItem.setQuantity(existItem.getQuantity() + quantity);
			existItem.setTotal(existItem.getPrice() * existItem.getQuantity());
		} else {// add new item into cart
			item.setQuantity(quantity);
			listItem.add(item);
		}
		int shopId = Integer
				.parseInt(session.getAttribute("shopId").toString());
		List<Category> listCategory = categoryService.getCategoryShop(shopId);
		modelAndView.addObject("product", product);
		modelAndView.addObject("listcategory", listCategory);
		modelAndView.setViewName(UtilityClass.VIEW_SHOP_PREVIEW);
		return modelAndView;
	}

	@RequestMapping(value = "/delcart", method = RequestMethod.GET)
	public @ResponseBody
	Cart deleteCart(@RequestParam String productId) {
		// Product product =
		// productService.getProduct(Integer.parseInt(productId));
		// Item item= new
		// Item(product.getId(),product.getName(),product.getPrice());
		cart.getItems().remove(Integer.parseInt(productId));
		return cart;
	}

	/*
	 * @RequestMapping(value = "/checkout", method = RequestMethod.GET) public
	 * String checkOut(Map<String, Object> map, HttpSession session) {
	 * StringBuilder detailPrice = new StringBuilder(); float total = 0;
	 * List<Item> listItem = cart.getItems(); for (Item item : listItem) {
	 * detailPrice.append(item.getTotal() + " + "); total += item.getTotal(); }
	 * 
	 * // delete 3 ky tu cuoi cung if (detailPrice.length() > 3)
	 * detailPrice.delete(detailPrice.length() - 3, detailPrice.length());
	 * map.put("detailPrice", detailPrice.toString()); map.put("total", total);
	 * map.put("LOGGED_IN",UtilityClass.LOGGED_IN);
	 * map.put("NOT_LOGGED_IN",UtilityClass.NOT_LOGGED_IN); Member member =
	 * (Member) session.getAttribute("userInfo"); if (member == null) {
	 * map.put("LOGIN_STATUS",UtilityClass.NOT_LOGGED_IN); }else{
	 * map.put("LOGIN_STATUS",UtilityClass.LOGGED_IN); } return
	 * UtilityClass.VIEW_SHOP_CHECKOUT; }
	 */
}
